Developing Serverless Solutions on AWS - Live Session Guide
This module covers Infrastructure as Code (IaC) frameworks for building and deploying serverless applications on AWS.
Resources:
MyBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: my-app-data
bucket = s3.Bucket(self, "MyBucket",
bucket_name="my-app-data",
removal_policy=RemovalPolicy.DESTROY
)
The foundation - all other frameworks compile down to CloudFormation templates.
| Concept | Description |
|---|---|
| Template | YAML/JSON file describing your infrastructure |
| Stack | A running instance of a template (collection of resources) |
| StackSet | Deploy stacks across multiple accounts and regions |
| Change Set | Preview changes before applying them |
| Drift Detection | Detect manual changes to stack resources |
Define cloud infrastructure using familiar programming languages. CDK synthesizes to CloudFormation.
cdk init app --language python # Initialize project cdk synth # Generate CloudFormation template cdk diff # Preview changes cdk deploy # Deploy to AWS cdk destroy # Tear down stack
| Level | Name | Description | Example |
|---|---|---|---|
| L1 | CFN Resources | Direct CloudFormation mapping (1:1) | CfnBucket |
| L2 | Curated Constructs | AWS-vetted with sensible defaults + helper methods | s3.Bucket() |
| L3 | Patterns | Multi-resource architectures in one construct | LambdaRestApi() |
from aws_cdk import Stack, aws_lambda as _lambda, aws_apigateway as apigw
from constructs import Construct
class MyServerlessStack(Stack):
def __init__(self, scope: Construct, id: str, **kwargs):
super().__init__(scope, id, **kwargs)
# Lambda function
handler = _lambda.Function(self, "Handler",
runtime=_lambda.Runtime.PYTHON_3_12,
code=_lambda.Code.from_asset("lambda"),
handler="index.handler",
)
# API Gateway (L3 pattern - creates REST API + integrations)
apigw.LambdaRestApi(self, "Endpoint", handler=handler)
An extension of CloudFormation specifically designed for serverless. Adds shorthand resource types that expand into full CloudFormation on deploy.
| SAM Type | Expands To |
|---|---|
| AWS::Serverless::Function | Lambda + Role + Permissions + Event Sources |
| AWS::Serverless::Api | API Gateway REST API + Stage + Deployment |
| AWS::Serverless::HttpApi | API Gateway HTTP API |
| AWS::Serverless::SimpleTable | DynamoDB table with single-attribute primary key |
| AWS::Serverless::StateMachine | Step Functions state machine |
| AWS::Serverless::LayerVersion | Lambda layer |
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31 # This line makes it SAM!
Resources:
MyFunction:
Type: AWS::Serverless::Function
Properties:
Handler: app.handler
Runtime: python3.12
MemorySize: 256
Timeout: 30
Events:
GetItems:
Type: Api
Properties:
Path: /items
Method: get
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref ItemsTable
ItemsTable:
Type: AWS::Serverless::SimpleTable
sam init # Create new SAM project (guided) sam build # Build artifacts (dependencies) sam local invoke # Test Lambda locally (Docker) sam local start-api # Run API Gateway locally sam local generate-event # Generate sample events (S3, SQS, etc.) sam validate # Validate template sam deploy --guided # Deploy to AWS (interactive) sam logs -n MyFunction # Tail CloudWatch Logs sam sync --watch # Hot-reload changes (dev mode)
Full-stack development platform for web and mobile apps. Combines frontend hosting with backend cloud services.
// amplify/data/resource.ts
import { defineData } from '@aws-amplify/backend';
import { a } from '@aws-amplify/data-schema';
const schema = a.schema({
Todo: a.model({
content: a.string(),
isDone: a.boolean(),
}).authorization(allow => [allow.owner()]),
});
export const data = defineData({ schema });
| Feature | CloudFormation | CDK | SAM | Amplify |
|---|---|---|---|---|
| Language | YAML/JSON | TypeScript, Python, Java, C#, Go | YAML/JSON | TypeScript |
| Paradigm | Declarative | Imperative | Declarative | Imperative |
| Scope | All AWS resources | All AWS resources | Serverless-focused | Full-stack web/mobile |
| Local testing | No | No (use SAM with CDK) | Yes (sam local) | Yes (sandbox) |
| Deploys via | Direct | CloudFormation | CloudFormation | CDK/CloudFormation |
| Best for | Enterprise IaC, compliance | Complex infra, reusable patterns | Lambda/API development | Frontend + backend rapid dev |
Key insight: All roads lead to CloudFormation. CDK, SAM, and Amplify all generate CloudFormation templates for deployment.
# Step 1: Create a new SAM project sam init --runtime python3.12 --name my-serverless-api --app-template hello-world # Step 2: Explore the generated files cd my-serverless-api cat template.yaml # SAM template cat hello_world/app.py # Lambda code # Step 3: Test locally sam build sam local invoke HelloWorldFunction sam local start-api # Start local API on port 3000 # In another terminal: curl http://localhost:3000/hello # Step 4: Deploy to AWS sam deploy --guided # Follow prompts: stack name, region, confirm changes # Step 5: Test deployed API curl https://<API_ID>.execute-api.us-west-2.amazonaws.com/Prod/hello/ # Step 6: View logs sam logs -n HelloWorldFunction --tail # Step 7: Cleanup sam delete --stack-name my-serverless-api
Developing Serverless Solutions on AWS - Module 4 | Live Session Guide
Last updated: June 2026